// 路由文件
import { createRouter, createWebHistory } from "vue-router";

import Login from "../view/Login.vue"

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/',
        name: 'Home',
        component: () => import('../view/Home.vue'),
        meta:{requireAuth: true},
        children: [
            {
                path: '/',
                name: 'Index',
                component: () => import('../view/Index.vue'),
                meta:{requireAuth: true}
            },
            {
                path: '/userList',
                name: 'UserList',
                component: () => import('../view/user/UserList.vue'),
                meta:{requireAuth: true}
            },
            {
                path: '/roleList',
                name: 'RoleList',
                component: () => import('../view/user/RoleList.vue'),
                meta:{requireAuth: true}
            },
            {
                path: '/apiList',
                name: 'ApiList',
                component: () => import('../view/user/ApiList.vue'),
                meta:{requireAuth: true}
            },
            {
                path: '/log',
                name: 'Log',
                component: () => import('../view/log/Log.vue'),
                meta:{requireAuth: true}
            },
            {
                path: '/editBlog',
                name: 'EditBlog',
                component: () => import('../view/blog/EditBlog.vue'),
                meta:{requireAuth: true}
            },
            {
                path: '/managerBlog',
                name: 'ManagerBlog',
                component: () => import('../view/blog/ManagerBlog.vue'),
                meta:{requireAuth: true}
            },
            {
                path: '/viewBlog/:id',
                name: 'ViewBlog',
                props: true,
                component: () => import('../components/ViewBlog.vue'),
                meta:{requireAuth: true}
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})


router.beforeEach((to,from)=>{
    if(to.meta.requireAuth) {
        let token = localStorage.getItem('auth-system-token');
        let isLogin = localStorage.getItem('auth-system-login');
        if(!token||!isLogin){
            return {
                path: '/login'
            }
        }
    }
})

export default router;